﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System.Runtime.Serialization;
using System;
using System.Xml.Serialization;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;
using System.Collections.ObjectModel;

namespace BrainTechLLC
{
    /// <summary>
    /// Ability enable commands and have properties.  i.e. Legs may enable walking.  Stalky legs may be stronger in combat, and long legs may allow faster running speeds, etc
    /// </summary>
    [Serializable]
    [DataContract]
    [KnownType("ReturnAllAbilityTypes")]
    public class Ability : GameThingBaseClass, IAbility
    {
        public string AbilityName
        {
            get
            {
                if (_name == null)
                    Interlocked.CompareExchange<NameAndDescription>(ref _name, new NameAndDescription(), null);

                return _name.Name;
            }
            set
            {
                if (_name == null)
                    Interlocked.CompareExchange<NameAndDescription>(ref _name, new NameAndDescription(), null);

                _name.Name = value;
            }
        }

        public static Collection<Type> ReturnAllAbilityTypes()
        {
            Collection<Type> results = new Collection<Type>();
            List<ITypeDescription> descs = TypeProvider.GetInstance().AllTypesThatImplement("IAbility");
            if (descs.Count == 0)
            {
                DLLContents dll = new DLLContents() { DLLName = "BrainTechLLC.Silverlight.Core" };
                Type[] types = Assembly.GetExecutingAssembly().GetTypes();

                foreach (var type in types)
                {
                    dll.TypesInDLL.Add(type, new TypeDescription("BrainTechLLC.Silverlight.Core", type));
                    TypeProvider.GetInstance()._typeLookup.AddOrSet(type.FullName, type);
                }

                TypeProvider.GetInstance()._dllContentList.Add(dll);
                descs = TypeProvider.GetInstance().AllTypesThatImplement("IAbility");
            }
            foreach (var desc in descs)
            {
                results.Add(desc.ClassType);
            }
            return results;
        }

        public string EnableCommandsList
        {
            get
            {
                List<Command> cmds = EnablesCommands;
                if (cmds != null)
                {
                    return cmds.BuildList((cmd) => cmd.FirstWords.BuildList((w) => w));
                }
                return string.Empty;
            }
        }

        [XmlIgnore]
        public virtual List<Command> EnablesCommands
        {
            get { return null; }
        }
    }
}
